package com.qiche.controller.mini;

import com.aliyuncs.exceptions.ClientException;
import com.qiche.aspect.Result;
import com.qiche.dto.StoreMemberDto;
import com.qiche.enums.RedisKeyPrefixEnum;
import com.qiche.enums.ResultEnum;
import com.qiche.model.ProductCategory;
import com.qiche.model.Region;
import com.qiche.model.StoreMember;
import com.qiche.redis.RedisService;
import com.qiche.service.ProductCategoryService;
import com.qiche.service.RegionService;
import com.qiche.service.StoreMemberService;
import com.qiche.util.AliyunSmsUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author chendezhi
 * @date 2018/3/28 11:21
 * <p>
 * 微信小程序会员端
 */
@RestController
@RequestMapping("/mini/member")
public class MemberMiniController {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private HttpSession session;
   @Autowired
   private RedisService redisService;
    @Autowired
    private RegionService regionService;
    @Autowired
    private ProductCategoryService productCategoryService;
    @Autowired
    private StoreMemberService storeMemberService;


    /**
     * 注册时,发送验证码
     *
     * @param phone
     * @return
     * @throws ClientException
     */
    @GetMapping(value = "/send/sms")
    public Result sendSms(@RequestParam("phone") String phone) throws ClientException {
    	StoreMember sessionMember = (StoreMember) session.getAttribute("member");
        if (sessionMember == null) {
            return Result.createByResultEnum(ResultEnum.MEMBER_LOGIN_PAST);
        }

        if (phone == null) {
            return Result.createByResultEnum(ResultEnum.PARAM_ERROR);
        }

        StoreMember member = storeMemberService.selectByPhoneAndMerchantId(phone, sessionMember.getMerchantId().intValue());
        if (member != null) {
            if (StringUtils.isNotEmpty(member.getUnionid())) {
                //说明 该手机号 已存在用户
                return Result.createByResultEnum(ResultEnum.MEMBER_EXIST_BY_PHONE);
            }
        }
        //发送验证码
        String randomCode = AliyunSmsUtils.getRandomCode();
        try {
            AliyunSmsUtils.sendSms(phone, randomCode);
            logger.info("{}发送验证码{}成功", phone, randomCode);
            redisService.set(RedisKeyPrefixEnum.SEND_MEMBER_WX_BIND_PHONE + phone, randomCode, 60L);
           // redisManager.set(RedisKeyPrefixEnum.SEND_MEMBER_WX_BIND_PHONE + phone, randomCode, 60);
            return Result.createByResultEnum(ResultEnum.SUCCESS);
        } catch (ClientException e) {
            logger.error(e.getMessage(), e);
            return Result.createByResultEnum(ResultEnum.FAILURE);
        }
    }

    /**
     * 获取所有 地市名称
     *
     * @return
     */
    @GetMapping("/region")
    public Result getAllRegion() {
    	StoreMember member = (StoreMember) session.getAttribute("member");
        if (member == null) {
            return Result.createByResultEnum(ResultEnum.MEMBER_LOGIN_PAST);
        }
        List<Region> regions = regionService.selectAll();
        return Result.createByResultEnum(ResultEnum.SUCCESS, regions);
    }

    /**
     * 获取所有产品分类
     * @return
     */
    @GetMapping("/category")
    public Result getAllCategory() {
        List<ProductCategory> list = productCategoryService.selectAll();
        return Result.createByResultEnum(ResultEnum.SUCCESS, list);
    }

    /**
     * B端添加客户
     * @param storeMemberDto
     * @return
     */
    @PostMapping("/addStoreMember")
    public Result addStoreMember(@RequestBody @Validated StoreMemberDto storeMemberDto,BindingResult result) { if (result.hasErrors()) {
            return Result.createByResultEnum(ResultEnum.PARAM_ERROR);
        }
       return storeMemberService.saveStoreMember(storeMemberDto);
    }

}
